﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HardToolApp.Model
{
    public enum GPIOMode
    {
        Input,
        InputUp,
        InputDown,
        Output,
        OutputOD
    }

    public class GpioModel : ObservableObject
    {

        public GPIO[] GPIOs { get; set; } = new GPIO[4];
        public GpioModel()
        { 
            for (int i = 0; i < GPIOs.Length; i++) 
            {
                GPIOs[i] = new GPIO((byte)i);
            }
        }
        public enum GPIOType
        {
            MODE_SET = 1,
            HL_CHANGE,
            SET_LEVEL,
            READ_LEVEL,
        }
        public class GPIO: Peripheral
        {
            static String[] PinName = { "PB4", "PB5", "PB10", "PB11" };
            public byte ID { get; set; }
            private GPIOMode _mode;
            public GPIOMode Mode { get { return _mode; } set {
                    SetProperty(ref _mode,value);
                    if (_mode == GPIOMode.Output || _mode == GPIOMode.OutputOD) CanSet = true;
                    else CanSet = false;
                    } }
            private bool? _startLevel = false;
            public bool? StartLevel { get { return _startLevel; } set { SetProperty(ref _startLevel, value); } }
            UInt16 _firstHoldTime = 1000;
            public UInt16 FirstHoldTime { get { return _firstHoldTime; } set { SetProperty(ref _firstHoldTime, value); } }
            UInt16 _secondHoldTime = 1000;
            public UInt16 SecondHoldTime { get { return _secondHoldTime; } set { SetProperty(ref _secondHoldTime, value); } }
            UInt16 _repeatTimes;
            public UInt16 RepeatTimes { get { return _repeatTimes; } set { SetProperty(ref _repeatTimes, value); } }
            bool? _level = false;
            public bool? Level { get { return _level; }set { SetProperty(ref _level, value); } }
            bool? _canSet = false;
            public bool? CanSet { get { return _canSet; } set { SetProperty(ref _canSet, value); } }
            bool? needWait = false;
            public bool? NeedWait { get => needWait;set=>SetProperty(ref needWait, value); }

            public GPIO(byte id) : base(CommCmd.COMM_GPIO)
            {
                ID = id;
            }
            public GPIO(): base(CommCmd.COMM_GPIO)
            {
                ID = 0;
            }
            public override string ToString()
            {
                switch (subCmd)
                {
                    case 1:
                        return "改变"+ PinName[ID] + "参数:" + Mode.ToString() + "\n";
                    case 2:
                        return PinName[ID] + "高低电平转换，起始" +(StartLevel==true?"高":"低")+"电平,第一段持续" +FirstHoldTime.ToString()+"ms,第二段持续"+ SecondHoldTime.ToString() + "\n";
                    case 3:
                        return "设置" + PinName[ID] + (Level==true?"高":"低") + "电平\n";
                    case 4:
                        return "读取" + PinName[ID] + (Level == true ? "高" : "低") + "电平\n";
                    default:
                        return "未知命令" + "\n";
                }
            }
        }
    }
}
